<?php
	class AdsAction extends LilAdsAppModel {
		var $name = 'AdsAction';
		
		var $date_regex = '%^(\d{4})-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$%';
 
		function getStats(&$params, $len = 10) {
			if (isset($params['start']) && !preg_match($this->date_regex, $params['start'])) {
				unset($params['start']);
			}
			if (isset($params['end']) && !preg_match($this->date_regex, $params['end'])) {
				unset($params['end']);
			}

			$conditions = array();
			if (!empty($params['campaign_id'])) $conditions['ads_campaign_id'] = (int)$params['campaign_id'];
			if (!empty($params['banner_id']))   $conditions['ads_banner_id'] = (int)$params['banner_id'];
			if (!empty($params['start']))       $conditions[] = array('date_hour >=' => $params['start']);
			if (!empty($params['end']))         $conditions[] = array('date_hour <=' => $params['end']);
			
			return $this->find('all', array(
				'fields'     => array(
					'SUM(clicks) AS clicks',
					'SUM(views) AS views',
					'LEFT(date_hour, ' . $len . ') AS date_hour'
				), 
				'conditions' => $conditions, 
				'order'      => 'date_hour',
				'group'      => 'LEFT(date_hour, ' . $len . ')'
			));
		}
	}
?>